# -*- coding: utf-8 -*-
__author__ = 'sunnychou'
__date__ = '2019/8/30 15:30'

'''
销售发票
'''

from common.orm_helper import *
from common.excel_helper import ExcelReader
from k3_data_report.import_tb.utils import fetch_tb_unique_his_data
from  common.logger_helper import  g_wlogger

##########################################
# 4 导入k3基础表  销售发票  BEGIN
##########################################
class  XsfpList(Base):
    __tablename__ = "k3_xsfp"  # 自定义数据库表名称
    id = Column(Integer, primary_key=True)  # 自增
    sjsj = Column(String(10), verbose_name='年月')
    kprq = Column(String(20), verbose_name='开票日期')
    yskrq = Column(String(80), verbose_name='应收款日期')
    ghdw = Column(String(100), verbose_name='购货单位')
    jsfs = Column(String(50), verbose_name='结算方式')
    fpcode = Column(String(50), verbose_name='发票号')
    dep = Column(String(50), verbose_name='部门')
    ywy = Column(String(20), verbose_name='业务员')
    bibie = Column(String(20), verbose_name='币别')
    huilv =  Column(String(20), verbose_name='汇率')
    chanpmc = Column(String(100), verbose_name='产品名称')
    ggxh  = Column(String(100), verbose_name='规格型号')
    dw = Column(String(100), verbose_name='单位')
    number = Column(String(50), verbose_name='数量')
    wsdj = Column(String(50), verbose_name='未税单价')
    se = Column(String(50), verbose_name='税额')
    jshj = Column(String(100), verbose_name='价税合计')
    cpcdm = Column(String(100), verbose_name='产品长代码')
    hsdj = Column(String(20), verbose_name='含税单价')
    ygjsl =  Column(String(20), verbose_name='已钩稽数量')
    ygjje = Column(String(20), verbose_name='已钩稽金额')
    bhsje = Column(String(20), verbose_name='不含税金额')
    pp =  Column(String(30), verbose_name='品牌')

    def __str__(self):
        return  self.ghdw

def  xiaoshoufap_process(excel_name, sheet_name):
    '''
    销售发票处理过程
    :param excel_name:
    :param sheet_name:
    :return:
    '''
    ereader = ExcelReader(excel_name, sheet_name=sheet_name)
    headers = ereader.get_excel_headers()
    data_lists = ereader.get_excel_data_list()
    print(f"total data len: {len(data_lists)}")
    danju_date = data_lists[0][0]
    danju_month = "%s-%s" % (danju_date.split("-")[0], danju_date.split("-")[1])
    unique_select_sql = "select  distinct sjsj  from k3_xsfp"
    unique_set = fetch_tb_unique_his_data(unique_select_sql)
    if danju_month in unique_set:
        #print(f"xiaoshoufap_process month:{danju_month} already existed.")
        already_info = f"waigoud_process month:{danju_month} already existed."
        g_wlogger.winfo(already_info)
        return
    for data_list in data_lists:
        ghdw = data_list[2]
        if ghdw == "":
            continue
        xsfp = XsfpList(sjsj=danju_month, kprq= data_list[0], yskrq=data_list[1], ghdw=data_list[2],
                                   jsfs=data_list[3], fpcode=data_list[4],dep=data_list[5], ywy=data_list[6],
                                   bibie=data_list[7], huilv=data_list[8],chanpmc=data_list[9],ggxh=data_list[10],
                                    dw = data_list[11],number=data_list[12],wsdj=data_list[13],se=data_list[14],
                                  jshj = data_list[15],cpcdm=data_list[16],hsdj=data_list[17], ygjsl=data_list[18],
                                  ygjje = data_list[19], bhsje=data_list[20],pp=data_list[21])
        session.add(xsfp)
        session.commit()

    #print(f"xiaoshoufap_process {danju_month}销售发票处理完毕！")

    xsfp_succ = f"销售发票 文件：{excel_name}， 日期：{danju_month}处理完毕！"
    g_wlogger.winfo(xsfp_succ)



def xiaoshoufap_process_main(excel_name, sheet_name):
    try:
        xiaoshoufap_process(excel_name, sheet_name)
        return True
    except Exception as e:
        g_wlogger.werror("xiaoshoufap_process_main has error:{e} with excel:{excel_name}")
        return False

##########################################
# 4 导入k3基础表  销售发票  END
##########################################




